var CFG = {
	TIMEOUT: 200000 // 超时时间
};

var href = window.location.href;
var EVT = '';
var _evt = function() {
	if (href.indexOf('https://') > -1) {
		// 测试环境
		if (href.indexOf('https://test') > -1 && href.indexOf('https://test.') == -1) {
			EVT = 'https://test';
			// dev环境
		} else if (href.indexOf('https://dev') > -1 && href.indexOf('https://dev.') == -1) {
			EVT = 'https://dev';
		} else {
			EVT = 'https://'
		}
		return EVT;
	} else {
		// 测试环境
		if (href.indexOf('http://test') > -1 && href.indexOf('http://test.') == -1) {
			EVT = 'http://test';
			// dev环境
		} else if (href.indexOf('http://dev') > -1 && href.indexOf('http://dev.') == -1) {
			EVT = 'http://dev';
		} else {
			EVT = 'http://'
		}
		return EVT;
	}
};

var _fn = {
	href: href,
	HOST: EVT + 'adminapi.inno-design.org.cn:8080', //这里设置接口域名
	// HOST: EVT + '152.136.124.214:8080', //这里设置接口域名
	// HOST1: EVT + 'nebula.dmall.com', //设置多个接口域名
	// 装饰参数
	decorateParam: function(param) {
		return param;
	},
	// 统一回调方案
	callbackProxy: function(data, callback) {
		// 这里可以做统一拦截方案
		if (data.code == "2000") {
			top.location.href = '../../login.html';
			//如果未登录
			//kayak.router.go(api.EVT + "erp.dmall.com/login?returnUrl=" + window.location.href);
			return;
		}

		// 这里可以做统一拦截方案
		if (typeof callback == 'function') {
			callback(data);
		}
	},
	// 获取需要的url
	getUrl: function(str) {
		var url = '';
		if (!str) {
			return str;
		}
		if (str.indexOf('http://') > -1 || str.indexOf('https://') > -1) { //传入的是完整的url
			url = str;
		} else { //传入的是字符串，需要在API中寻找
			url = api[str];
		}
		return url;
	},
	mixUrl: function(host, url) {
		var p;

		if (!host || !url || _fn.isEmptyObject(url)) {
			return;
		}
		url.EVT = _evt();
		for (p in url) {
			if (url[p].indexOf('http') == -1) {
				url[p] = url.EVT + host + url[p];
			}
		}
		return url;
	},
	//判断是否空对象
	isEmptyObject: function(obj) { //判断空对象
		if (typeof obj === "object" && !(obj instanceof Array)) {
			var hasProp = false;
			for (var prop in obj) {
				hasProp = true;
				break;
			}
			if (hasProp) {
				return false;
			}
			return true;
		}
	}
}

var AJAX = null;
var cgiMain = {};
console.log($);
//var $ = null;
//layui.use(['jquery'], function() {
	//$ = layui.$;
	AJAX = {
		//get请求
		query: function() {
			var url,
				param,
				type,
				callback;
			if (arguments.length == 3) {
				url = arguments[0];
				type = 'json';
				param = arguments[1];
				callback = arguments[2];
			} else if (arguments.length == 4) {
				url = arguments[0];
				type = arguments[1];
				param = arguments[2];
				callback = arguments[3];
			} else {
				return;
			}

			url = _fn.getUrl(url);
			param = param || {};
			param = _fn.decorateParam(param);
			$.ajax({
				url: url,
				dataType: 'json',
				data: param,
				xhrFields: {
					withCredentials: true
				},
				contentType: false,
				type: 'GET',
				cache: false,
				timeout: CFG.TIMEOUT,
				success: function(res) {
					if (typeof callback == 'function') {
						_fn.callbackProxy(res, callback);
					}
				},
				error: function() {
					var data = {
						code: '-1',
						result: '加载数据失败',
						data: {}
					};
					if (typeof callback == 'function') {
						_fn.callbackProxy(data, callback);
					}
				}
			});
		},
		//post请求
		post: function() {
			var url,
				param,
				type,
				callback,
				async = true;
			if (arguments.length == 3) {
				url = arguments[0];
				type = 'json';
				param = arguments[1];
				callback = arguments[2];
			} else if (arguments.length == 4) {
				url = arguments[0];
				type = 'json';
				param = arguments[1];
				callback = arguments[2];
				async = arguments[3];
			} else {
				return;
			}

			url = _fn.getUrl(url);
			param = param || {};
			param = _fn.decorateParam(param);
			$.ajax({
				url: url,
				dataType: 'json',
				data: param,
				async: async,
				xhrFields: {
					withCredentials: true
				},
				type: 'POST',
				cache: false,
				timeout: CFG.TIMEOUT,
				success: function(res) {
					if (typeof callback == 'function') {
						_fn.callbackProxy(res, callback);
					}
				},
				error: function() {
					var data = {
						code: '-1',
						result: '加载数据失败',
						data: {}
					};
					if (typeof callback == 'function') {
						_fn.callbackProxy(data, callback);
					}
				}
			});
		},
		//文件上传
		postFile: function(option) {
			var url,
				param,
				file,
				type,
				callback;
			if (arguments.length == 3) {
				url = api[arguments[0]];
				type = 'json';
				param = arguments[1].param;
				file = arguments[1].file;
				callback = arguments[2];
			} else {
				return;
			}
			url = _fn.getUrl(url);
			var formData = new FormData();
			formData.append("file", file);
			for (var i in param) {
				formData.append(i, param[i]);
			}

			$.ajax({
				url: url,
				type: "POST",
				data: formData,
				xhrFields: {
					withCredentials: true
				},
				/**
				 *必须false才会自动加上正确的Content-Type
				 */
				contentType: false,
				/**
				 * 必须false才会避开jQuery对 formdata 的默认处理
				 * XMLHttpRequest会对 formdata 进行正确的处理
				 */
				processData: false,
				success: function(res) {
					if (typeof callback == 'function') {
						_fn.callbackProxy(res, callback);
					}
				},
				error: function(res) {
					if (typeof callback == 'function') {
						_fn.callbackProxy(res, callback);
					}
				}
			});
		}
	};

	cgiMain = $.extend(cgiMain, _fn.mixUrl(_fn.HOST, CGI.URL));
	cgiMain = $.extend(cgiMain, _fn.mixUrl(_fn.HOST1, CGI.URL1));
	//console.log(cgiMain);
//});
